//
//  JXFirstHomePageController.m
//  JiaoX
//
//  Created by yinqy on 15/1/30.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXFirstHomePageController.h"
#import "HomeDataTool.h"
#import "SGFocusImageItem.h"
#import "MJRefresh.h"
#import "HomeReviewsCell.h"
#import "LeftLineView.h"
#import "HomeFirstPageHeaderView.h"
#import "JXReviewsController.h"
#import "JXProdDetailCtl.h"
#import "JXPostsDetailCtl.h"
#import "PostsItemFrame.h"
#import "PostsItemCell.h"

#define kSectionHeight [UIFont systemFontOfSize:[DimensUtils getScaleX:14]].lineHeight + JXPublicInset*2

@interface JXFirstHomePageController ()<SGFocusImageFrameDelegate,UITableViewDelegate,UITableViewDataSource,HomeFirstPageHeaderViewDelegate>
{
    HomeFirstPageInfo *_pageInfo;
    NSMutableArray *_reviewsArray;
    NSMutableArray *_postsArray;
    HomeFirstPageHeaderView *_headerView;
    UIButton *_toTopBtn;
}

@end

@implementation JXFirstHomePageController

-(instancetype)initWithFirstPageInfo:(HomeFirstPageInfo *)pageInfo
{
    self = [super init];
    if (self) {
        _pageInfo = pageInfo;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _reviewsArray = [NSMutableArray array];
    _postsArray = [NSMutableArray array];
    CGRect tableViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.height-NavHeight-49);
    _tableView = [[UITableView alloc]initWithFrame:tableViewFrame];
    [_tableView setBackgroundColor:JXColor(238, 238, 238)];
    _tableView.delegate = self;
    [_tableView setSeparatorColor:JXColor(242, 242, 242)];
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    _toTopBtn = [[UIButton alloc]init];
    _toTopBtn.frame = CGRectMake(JXScreenW-50, _tableView.height-60, 40, 40);
    [_toTopBtn setHidden:YES];
    [_toTopBtn addTarget:self action:@selector(toTop:) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:_toTopBtn aboveSubview:self.tableView];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, JXScreenW, 0.01)]];
    
    [self inflateData:_pageInfo];
}

-(void)headerRefresh{
    [self request];
}

-(void)toTop:(UIButton *)toTopBtn{
    [self.tableView setContentOffset:CGPointZero animated:YES];
}

-(void)request{
    [HomeDataTool getFirstHomePageData:^(HomeFirstPageInfo *result) {
        [self.tableView headerEndRefreshing];
        if ([Utils isBlankString:result.data]) {
            [self inflateData:result];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [self.tableView headerEndRefreshing];
        UIViewController *c = [[self.view superview] viewController];
        [DialogUtils showTipsWithView:c.navigationController.view String:@"加载失败"];
    }];
}

-(void)inflateData:(HomeFirstPageInfo *)result{
    _pageInfo = result;
    [_toTopBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:result.backTopImg] forState:UIControlStateNormal];
    _headerView = [[HomeFirstPageHeaderView alloc]initWithHomeFirstPageInfo:result];
    _headerView.bannerView.delegate = self;
    
    _headerView.delegate = self;
    [self.tableView setTableHeaderView:_headerView];
    [_reviewsArray removeAllObjects];
    [_reviewsArray addObjectsFromArray:result.reviewsList.list];
    
    
    [_postsArray removeAllObjects];
    for (BBSPosts *posts in result.postsList.list) {
        PostsItemFrame *frame = [[PostsItemFrame alloc]init];
        [frame setPosts:posts];
        [_postsArray addObject:frame];
    }
    [self.tableView reloadData];
}


#pragma mark - tableview 代理、数据源

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return _reviewsArray.count;
    }else if(section == 1){
        return _postsArray.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HomeReviewsCell *cell = [HomeReviewsCell cellWithTableView:tableView];
        [cell setReviews:_reviewsArray[indexPath.row]];
        return cell;
    }else if(indexPath.section == 1){
        PostsItemCell *cell = [PostsItemCell cellWithTableView:tableView];
        [cell setPostsItemFrame:_postsArray[indexPath.row]];
        return cell;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        if (_pageInfo.reviewsList==nil || _pageInfo.reviewsList.list.count == 0) {
            return nil;
        }else{
            UIView *view = [[UIView alloc]init];
            [view setBackgroundColor:JXColor(238, 238, 238)];
            
            UIView *child = [[UIView alloc]initWithFrame:CGRectMake(0, 10, JXScreenW, kSectionHeight-10)];
            [child setBackgroundColor:[UIColor whiteColor]];
            
            LeftLineView *left = [[LeftLineView alloc]initWithTitleFontSize:[DimensUtils getScaleX:14]];
            [left setText:_pageInfo.reviewsList.title];
            left.x = JXPublicInset;
            left.y = JXPublicInset;
            
            [child addSubview:left];
            [view addSubview:child];
            
            return view;
        }
    }else if (section==1){
        if (_pageInfo.postsList == nil || _pageInfo.postsList.list.count==0) {
            return nil;
        }else{
            UIView *view = [[UIView alloc]init];
            [view setBackgroundColor:JXColor(238, 238, 238)];
            
            UIView *child = [[UIView alloc]initWithFrame:CGRectMake(0, 10, JXScreenW, kSectionHeight-10)];
            [child setBackgroundColor:[UIColor whiteColor]];
            
            LeftLineView *left = [[LeftLineView alloc]initWithTitleFontSize:[DimensUtils getScaleX:14]];
            [left setText:_pageInfo.postsList.title];
            left.x = JXPublicInset;
            left.y = JXPublicInset;
            
            [child addSubview:left];
            [view addSubview:child];
            
            return view;
        }
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        UIViewController *c = [[self.view superview] viewController];
        HomeReviews *reviews = _reviewsArray[indexPath.row];
        [c.navigationController pushViewController:[JXReviewsController initWithReviewId:reviews.reviewsId title:reviews.labelTitle reviewsTitle:reviews.title shareImgUrl:reviews.img] animated:true];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }else if (indexPath.section==1){
        PostsItemFrame *frame = _postsArray[indexPath.row];
        JXPostsDetailCtl *ctl = [[JXPostsDetailCtl alloc]initWithBBSPosts:frame.posts];
        [[[self.view superview] viewController].navigationController pushViewController:ctl animated:YES];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return HomeReviewsViewHeight;
    }else if(indexPath.section == 1){
        PostsItemFrame *frame = _postsArray[indexPath.row];
        return frame.frame.size.height;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        if (_pageInfo.reviewsList==nil || _pageInfo.reviewsList.list.count == 0) {
            return 0;
        }else{
            return kSectionHeight;
        }
    }else{
        if (_pageInfo.postsList==nil || _pageInfo.postsList.list.count == 0) {
            return 0;
        }else{
            return kSectionHeight;
        }
    }
    return kSectionHeight;
}

-(void)viewWillLayoutSubviews{
    [Utils setSeparatorInsetWithTableView:_tableView insets:UIEdgeInsetsMake(0, JXPublicInset, 0, 0)];
}

-(void)dealUrl:(NSString *)url{
    UIViewController *c = [[self.view superview] viewController];
    [Utils dealUrlWithController:c url:url title:@"" fromWebView:false];
}

- (void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item{
    //点击了顶部的topBanner
    [self dealUrl:item.title];
}

- (void)sectionOneClick:(NSString *)urlStr{
    [self dealUrl:urlStr];
}
- (void)sectionTwoClick:(NSString *)urlStr{
   [self dealUrl:urlStr];
}
- (void)sectionThreeClick:(NSString *)urlStr{
   [self dealUrl:urlStr];
}
- (void)sectionFourClick:(ProductItemInfo *)prodItem{
    //要在这里处理商品点击
    JXProdDetailCtl *proddc = [[JXProdDetailCtl alloc]initWithPordId:[NSString stringWithFormat:@"%d",prodItem.prodId]];
    UIViewController *c = [[self.view superview] viewController];
    [c.navigationController pushViewController:proddc animated:YES];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _tableView){
        CGFloat sectionHeaderHeight = kSectionHeight;
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
    if(scrollView.contentOffset.y>scrollView.height*2){
        [_toTopBtn setHidden:NO];
    }else{
        [_toTopBtn setHidden:YES];
    }
}


@end
